﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMVC.Model;
using MyMVC.BLL;
using System.Security.Cryptography;
using System.Web.Security;

namespace MyMVC.Controllers
{
    public class LoginController : Controller
    {
        UsersManager um = new UsersManager();

        /// <summary>
        /// 跳转登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("Login");
        }

        [HttpPost]
        /// <summary>
        /// 执行登录操作
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            //获取数据
            Users user = new Users()
            {
                UName = Request.Params["username"],
                UPwd = Request.Params["password"]
            };
            //使用MD5加密，和数据库加密后的数据作对比
            user.UPwd = Md5(user.UPwd);
            //业务处理
            user = um.login(user);
            if (user != null)
            {
                //ViewData["info"] = "Hello:" + user.UName;
                //跳转到 Home控制器，调用Home控制器的getlist方法
                return RedirectToAction("List", "Home");
            }
            else
            {
                ViewData["info"] = "用户名或密码错误！";
            }
            return View();
        }

        /// <summary>
        /// 跳转注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult insert()
        {
            return View("insert");
        }

        /// <summary>
        /// 执行注册操作
        /// </summary>
        /// <returns></returns>
        public ActionResult inserts() {
            //获取数据
            Users user = new Users()
            {
                UName = Request.Params["name"],
                UPwd = Request.Params["pwd"]
            };
            //使用MD5加密
            user.UPwd = Md5(user.UPwd);
            //业务处理
            user = new UsersService().insert(user);
            if (user != null)
            {
                ViewData["info"] = "注册成功！";
            }
            else
            {
                ViewData["info"] = "注册失败！";
            }
            return View("Login");
        }

        /// <summary>
        /// 注销操作
        /// </summary>
        /// <returns></returns>
        public ActionResult logout()
        {
            //清除session
            Session.Clear();
            return View("Login");
        }

        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="originStr"></param>
        /// <returns></returns>
        public static string Md5(string originStr)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string hashStr = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(originStr)));
            hashStr = hashStr.Replace("-", "");
            hashStr = hashStr.ToLower();
            return hashStr;
        }
    }
}